{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9eccf1b3",
   "metadata": {},
   "source": [
    "# Viewing the segmentation dataset\n",
    "In this example notebook we show how to view the segmentation information in the segmentation dataset.  We provide some example files in the folder `example_files/step_examples`.   For more files please download the latest Fusion 360 Gallery dataset from `https://github.com/AutodeskAILab/Fusion360GalleryDataset`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b40f046",
   "metadata": {},
   "source": [
    "First we need to make sure we are in the correct directory."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "55892f50",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "if os.path.isfile('../models/brepnet.py'):\n",
    "    # We are in the notebooks directory.  Change to the root\n",
    "    os.chdir('../')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9a566572",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "\n",
    "# This viewer utility will allow us to view the segmentation\n",
    "from visualization.jupyter_segmentation_viewer import JupyterSegmentationViewer\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "224de4b2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We found 25 example files\n"
     ]
    }
   ],
   "source": [
    "step_folder = Path(\"./example_files/step_examples\")\n",
    "seg_folder = step_folder\n",
    "step_file_stems = [ f.stem for f in step_folder.glob(\"*.stp\")]\n",
    "print(f\"We found {len(step_file_stems)} example files\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f60517c2",
   "metadata": {},
   "source": [
    "Here you can select the index of the file you would like to view"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8b77e9b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Viewing example 142473_f37f7cba_0\n"
     ]
    }
   ],
   "source": [
    "example_index = 0\n",
    "file_stem = step_file_stems[example_index]\n",
    "print(f\"Viewing example {file_stem}\")\n",
    "viewer = JupyterSegmentationViewer(file_stem, step_folder, seg_folder)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89438e39",
   "metadata": {},
   "source": [
    "We can view the solid like this.   For some reason it can be quite slow to render the solids in a notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2f6c1135",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "068375981651401c949f900e6d883337",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HBox(children=(Checkbox(value=True, description='Axes', layout=Layout(height='au…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "viewer.view_solid()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "625865f5",
   "metadata": {},
   "source": [
    "And then visualize the ground truth segmentation like this"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "75679e53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7631c2b7e0d34610b9de76e9898bd6c9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(VBox(children=(HBox(children=(Checkbox(value=True, description='Axes', layout=Layout(height='au…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "viewer.view_segmentation()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
